import axios from 'axios'
import { AxiosInstance } from 'axios' //导入instance和config的类型（axios里面有对应的类型注解）
import { ElLoading } from 'element-plus'
import { ILoadingInstance } from 'element-plus/lib/el-loading/src/loading.type'
import type { WXRequestConfig, WXRequestInterceptors } from './type'
class WXRequest {
  instance: AxiosInstance
  interceptors?: WXRequestInterceptors
  loading?: ILoadingInstance
  showLoading: boolean
  constructor(config: WXRequestConfig) {
    this.instance = axios.create(config)
    this.showLoading = config.showLoading ?? true //默认显示loading
    this.interceptors = config.interceptors

    // 从config中取出的拦截器是我们对应的实例的拦截器
    this.instance.interceptors.request.use(
      this.interceptors?.requestInterceptor,
      this.interceptors?.requestInterceptorCatch
    ) //请求拦截

    this.instance.interceptors.response.use(
      this.interceptors?.responseInterceptor,
      this.interceptors?.responseInterceptorCatch
    ) //响应拦截

    // 配置一个所有的实例都有的拦截器
    this.instance.interceptors.request.use(
      (config) => {
        // console.log('所有请求成功拦截')
        if (this.showLoading) {
          //是否显示loading
          this.loading = ElLoading.service({
            lock: true, //蒙版
            text: '正在加载中', //文字
            background: 'rgba(0, 0, 0, 0.5)' //遮罩背景颜色
          })
        }

        return config
      },
      (err) => {
        // console.log('所有请求失败拦截')
        return err
      }
    )
    this.instance.interceptors.response.use(
      (res) => {
        // console.log('所有响应成功拦截')
        // 请求成功，将loading关闭
        this.loading?.close()
        return res.data
      },
      (err) => {
        // console.log('所有响应失败拦截')
        this.loading?.close() //请求失败也将loading关闭，显示其他失败组件
        return err
      }
    )
  }
  request<T = any>(config: WXRequestConfig): Promise<T> {
    return new Promise((resolve, reject) => {
      if (config.interceptors?.requestInterceptor) {
        config = config.interceptors.requestInterceptor(config)
      }
      if (config.showLoading === false) {
        this.showLoading = config.showLoading
      }
      this.instance
        .request<any, T>(config)
        .then((res: any) => {
          if (config.interceptors?.requestInterceptor) {
            res = config.interceptors.requestInterceptor(res)
          }
          this.showLoading = true
          resolve(res) //将结果返回出去
        })
        .catch((err) => {
          this.showLoading = true
          reject(err)
          return err
        })
    })
  }
  get<T = any>(config: WXRequestConfig): Promise<T> {
    return this.request<T>({ ...config, method: 'GET' })
  }
  post<T = any>(config: WXRequestConfig): Promise<T> {
    return this.request<T>({ ...config, method: 'POST' })
  }
  delete<T = any>(config: WXRequestConfig): Promise<T> {
    return this.request<T>({ ...config, method: 'DELETE' })
  }
  patch<T = any>(config: WXRequestConfig): Promise<T> {
    return this.request<T>({ ...config, method: 'PATCH' })
  }
}
export default WXRequest
